using System;
using System.Data;
using System.Configuration;

using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using StillVoiceCommon;
using TN5Y.Server;
using TN5Y;
using Microsoft.ApplicationBlocks.Data;
using System.Data.SqlClient;
using System.Text;
using System.Collections.Generic;
using System.IO;
/// <summary>
/// Summary description for EnterpriseProgressReport
/// </summary>

namespace TeamTime.Classes
{

    public class EnterpriseProgressReport : StillVoiceCommon.DataTableRow
    {
        #region properties

        public string ConsultantFullNameAndID;
        #endregion

        public EnterpriseProgressReport()
        {
            this.initClass(TN5Y.Utilities.DBConnectionString, "EnterpriseProgressReport");
        }
        public new void load(string id)
        {
            base.load(id);
                        System.Data.SqlClient.SqlDataReader dr = SqlHelper.ExecuteReader(TN5Y.Utilities.DBConnectionString, CommandType.Text, 
                            "select * from ViewEnterpriseProgressReports where ID = @ID",
                            new SqlParameter("ID",id));
            
            if (dr.Read())
            {
                //usernameModifiedBy = dr["UserNameModifiedBy"].ToString();
                ConsultantFullNameAndID = dr["FullNameAndID"].ToString();
            }
            dr.Close();
            dr = null;
        }

        internal bool loadByKeyValues(int enterpriseID, DateTime reportDate)
        {
            bool ret = false;
            System.Data.SqlClient.SqlDataReader dr = SqlHelper.ExecuteReader(TN5Y.Utilities.DBConnectionString, CommandType.Text,
                "select ID from EnterpriseProgressReport where ReportDate = @ReportDate and EnterpriseID = @EnterpriseID",
                new SqlParameter("ReportDate", reportDate),
                new SqlParameter("EnterpriseID",enterpriseID));
            
            if (dr.Read())
            {
                if (dr[0] !=DBNull.Value)
                {

                    this.load(Convert.ToInt32(dr[0]));
                    ret = true;
                }
            }
            return ret;
        }
    }

    public class EnterpriseProgressReports
    {
        public DataSet searchEnterpriseProgressReports(string searchFor, string sortExpression)
        {
            StringBuilder sql = new StringBuilder("SELECT * FROM ViewEnterpriseProgressReports where 1=1 ");
            if (searchFor != "")
            {
                sql.Append(" and (FullName like '%'+@searchFor+'%' or");
                sql.Append(" PositionHeld like '%'+@searchFor+'%' ");
                
                sql.Append(") ");
            }

            if (sortExpression != "")
            {
                sql.Append(" order by " + sortExpression);
            }
            return SqlHelper.ExecuteDataset(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql.ToString(), 
                                                new SqlParameter("searchFor",searchFor));
        }

        public bool duplicateEnterpriseProgressReportExists(int EnterpriseProgressReport_PersonID, int EnterpriseID, string ID)
        {
            bool ret = false;
            string sql = "select ID from ViewEnterpriseProgressReports where PersonID = '" + EnterpriseProgressReport_PersonID + "' " +
                        " and EnterpriseID = " + EnterpriseID.ToString();

            if (ID != "") { sql += " and ID <> '" + ID + "'"; }//same name different id
            SqlDataReader dr;
            dr = SqlHelper.ExecuteReader(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql);
            ret = dr.Read();
            dr.Close();
            return ret;
        }

        public int getDuplicateEnterpriseProgressReportID(int enterpriseID, DateTime reportDate, string ID)
        {
            int ret = 0;
            string sql = "select ID from ViewEnterpriseProgressReports where Month(ReportDate) = @ReportMonth" +
                        " and Year(ReportDate) = @ReportYear " +
                        " and EnterpriseID = @EnterpriseID ";
            if (ID != "") { sql += " and ID <> @ID"; }//same name different id
            SqlDataReader dr;
            dr = SqlHelper.ExecuteReader(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql, 
                    new SqlParameter("EnterpriseID",enterpriseID),
                    new SqlParameter("ReportMonth",reportDate.Month),
                    new SqlParameter("ReportYear",reportDate.Year),
                    new SqlParameter("ID",ID));
            if (dr.Read())
            {
                ret = Convert.ToInt32(dr[0]);
            }
            dr.Close();
            return ret;
        }

        //internal string GetEnterpriseProgressReportEmail(Int32 EnterpriseProgressReportID)
        //{
        //    string ret = "";
        //    string sql = "select EmailAddress from ViewEnterpriseProgressReports where ID = @EnterpriseProgressReportID";

        //    SqlDataReader dr;
        //    dr = SqlHelper.ExecuteReader(TN5Y.Utilities.DBConnectionString, CommandType.Text, sql, 
        //        new SqlParameter("EnterpriseProgressReportID", EnterpriseProgressReportID));
        //    if (dr.Read())
        //    {
        //        ret = dr[0].ToString();
        //    }
        //    dr.Close();
        //    return ret;
        //}


    }
}
